package com.gouqi.search.manager;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gouqi.search.common.ErrorCode;
import com.gouqi.search.datasource.*;
import com.gouqi.search.exception.BusinessException;
import com.gouqi.search.exception.ThrowUtils;
import com.gouqi.search.model.dto.post.PostQueryRequest;
import com.gouqi.search.model.dto.search.SearchRequest;
import com.gouqi.search.model.dto.user.UserQueryRequest;
import com.gouqi.search.model.entity.Picture;
import com.gouqi.search.model.enums.SearchTypeEnum;
import com.gouqi.search.model.vo.PostVO;
import com.gouqi.search.model.vo.SearchVO;
import com.gouqi.search.model.vo.UserVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 搜索门面
 */
@Component
public class SearchFacade {

    @Resource
    private PostSource postSource;

    @Resource
    private UserSource userSource;

    @Resource
    private PictureSource pictureSource;

    @Resource
    private DataRegistry dataRegistry;

    /**
     * 分页获取列表（封装类）
     *
     * @param searchRequest
     * @param request
     * @return
     */
    public SearchVO searchAll(SearchRequest searchRequest, HttpServletRequest request) {

        String searchText = searchRequest.getSearchText();
        String type = searchRequest.getType();
        long current = searchRequest.getCurrent();
        long pageSize = searchRequest.getPageSize();

        SearchTypeEnum searchTypeEnum = SearchTypeEnum.getEnumByValue(type);
        ThrowUtils.throwIf(!StrUtil.isNotBlank(type), ErrorCode.PARAMS_ERROR);

        if (searchTypeEnum == null) {

            CompletableFuture<Page<UserVO>> userTask = CompletableFuture.supplyAsync(() -> {
                UserQueryRequest userQueryRequest = new UserQueryRequest();
                userQueryRequest.setUserName(searchText);
                Page<UserVO> userVOPage = userSource.doSearch(searchText, current, pageSize);
                return userVOPage;
            });

            CompletableFuture<Page<PostVO>> postTask = CompletableFuture.supplyAsync(() -> {
                PostQueryRequest postQueryRequest = new PostQueryRequest();
                postQueryRequest.setSearchText(searchText);
                Page<PostVO> postVOPage = postSource.doSearch(searchText, current, pageSize);
                return postVOPage;
            });

            CompletableFuture<Page<Picture>> pictureTask = CompletableFuture.supplyAsync(() -> {
                Page<Picture> picturePage = pictureSource.doSearch(searchText, 1, 10);
                return picturePage;
            });

            CompletableFuture.allOf(userTask, postTask, pictureTask);

            try {
                Page<UserVO> userVOPage = userTask.get();
                Page<PostVO> postVOPage = postTask.get();
                Page<Picture> picturePage = pictureTask.get();
                List<UserVO> userList = userVOPage.getRecords();
                List<PostVO> postList = postVOPage.getRecords();
                List<Picture> pictureList = picturePage.getRecords();

                SearchVO searchVO = new SearchVO();
                searchVO.setUserList(userList);
                searchVO.setPostList(postList);
                searchVO.setPictureList(pictureList);
                return searchVO;
            } catch (Exception e) {
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, "数据查询失败");
            }
        } else {
            SearchVO searchVO = new SearchVO();
            DataSource<?> dataSource = dataRegistry.getDataSourceByType(type);
            Page page = dataSource.doSearch(searchText, current, pageSize);
            searchVO.setDataList(page.getRecords());
            return searchVO;
        }
    }
}